package com.ruoyi.web.controller.store;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.stage.domain.StuStage;
import com.ruoyi.stage.service.IStuStageService;
import com.ruoyi.store.domain.StuQuestionStore;
import com.ruoyi.store.service.IStuQuestionStoreService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 题库管理
 */
@RestController
@RequestMapping("store/storelist")
public class StuQuestionStoreController extends BaseController {
    @Resource
    private  IStuQuestionStoreService stuQuestionStoreService;

    @Resource
    private IStuStageService stuStageService;
    @GetMapping("/list")
    @PreAuthorize("@ss.hasPermi('store:store:list')")
    public TableDataInfo list(StuQuestionStore stuQuestionStore)
    {
        startPage();
        List<StuQuestionStore> list = stuQuestionStoreService.selectStuQuestionStoreList(stuQuestionStore);
        return getDataTable(list);
    }

    /**
     * 显示题库详细信息,实现动态加载阶段
     */
    @GetMapping(value = {"/","/{storeId}"})
    @PreAuthorize("@ss.hasPermi('store:list:query')")
    public AjaxResult getInfo(@PathVariable(value = "storeId",required = false) Long storeId){
        AjaxResult ajax = AjaxResult.success();
        ajax.put("stages",stuStageService.selectAllStages());
        if (StringUtils.isNotNull(storeId)){
            StuQuestionStore stuQuestionStore = stuQuestionStoreService.selectStoreById(storeId);
            ajax.put(AjaxResult.DATA_TAG, stuQuestionStore);
            ajax.put("stageIds", stuStageService.selectStuStagesListByStoreId(storeId));
        }
        return ajax;
    }

    /**
     * 添加题库
     */
    @PostMapping
    @PreAuthorize("@ss.hasPermi('store:list:add')")
    public AjaxResult add(@RequestBody StuQuestionStore stuQuestionStore){
        if (UserConstants.NOT_UNIQUE.equals(stuQuestionStoreService.checkStoreNameUnique(stuQuestionStore)))
        {
            return AjaxResult.error("新增题库'" + stuQuestionStore.getStoreName() + "'失败，题库已存在");
        }
        stuQuestionStore.setCreateBy(getUsername());
        return toAjax(stuQuestionStoreService.insertStore(stuQuestionStore));
    }

    /**
     * 修改题库
     */
    @PutMapping
    @PreAuthorize("@ss.hasPermi('store:list:edit')")
    public  AjaxResult edit(@Validated @RequestBody StuQuestionStore stuQuestionStore){
        if (UserConstants.NOT_UNIQUE.equals(stuQuestionStoreService.checkStoreNameUnique(stuQuestionStore)))
        {
            return AjaxResult.error("修改题库'" + stuQuestionStore.getStoreName() + "'失败，题库名称已存在");
        }
        stuQuestionStore.setUpdateBy(getUsername());
        return toAjax(stuQuestionStoreService.updateStore(stuQuestionStore));
    }

    /**
     * 删除题库
     */
    @DeleteMapping("/{storeIds}")
    @PreAuthorize("@ss.hasPermi('store:list:remove')")
    public AjaxResult remove(@PathVariable Long[] storeIds)
    {
        return toAjax(stuQuestionStoreService.deleteStoreByIds(storeIds));
    }


    @Log(title = "题库管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, StuQuestionStore stuQuestionStore)
    {
        List<StuQuestionStore> list = stuQuestionStoreService.selectStuQuestionStoreList(stuQuestionStore);
        ExcelUtil<StuQuestionStore> util = new ExcelUtil<StuQuestionStore>(StuQuestionStore.class);
        util.exportExcel(response, list, "题库数据");
    }


}
